# InventoryUI.gd
extends CanvasLayer



func _ready():
	hide()  # 开始隐藏

	


func _input(event):                  #打开/关闭
	if event.is_action_pressed("bag_open"):
		GameUtiles.current_item = ""  #初始化选中物品
		if not visible and GameUtiles.move_allow:
			fill_in_bag()
			print("已填充")
		visible = not visible 
		GameUtiles.move_allow=false if visible else true

@onready var bag = $item_bag/Panel/GridContainer


func fill_in_bag():      
	#背包可视化，调用函数时绘制，建议后期制作合成系统时再次调用
	#初始化参数
	var container_number=1
	var item_list=GameUtiles.player_bag
	GameUtiles.current_grid="item0"
	
	#清空背包,再进行填充
	for grid in bag.get_children():
		if grid is PanelContainer:
			#清空当前格数据
			grid.item_in_grid = ""
			grid.amount_in_grid = 0
			for child in grid.get_children():
				#清空图像
				child.queue_free()
				
	
	for item in item_list:
		if item_list[item]>0:
			#设置可视化
			#添加图标
			var sprite=Sprite2D.new()
			sprite.texture = load("res://images/assets/"+str(item)+".png")
			sprite.position = Vector2(16,16)
			sprite.scale = Vector2(0.8,0.8)
			
			get_node("item_bag/Panel/GridContainer/item"+str(container_number)).add_child(sprite)
			
			
			#更新数字
			var number=Sprite2D.new()
			number.texture = load("res://images/system/numbers/"+str(item_list[item])+".png")
			number.position = Vector2(20,12)
			number.scale = Vector2(1,1)
			
			get_node("item_bag/Panel/GridContainer/item"+str(container_number)).add_child(number)
			
			
			var current_node=get_node("item_bag/Panel/GridContainer/item"+str(container_number))
				
			#修改仓库
			#同步单个grid的函数信息
			current_node.item_in_grid = item
			current_node.amount_in_grid = item_list[item]
			
			
			#准备下一次循环
			container_number += 1
			
